/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.jrwz.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.jrwz.server.ClientThread;
import l1j.jrwz.server.datatables.PetItemTable;
import l1j.jrwz.server.datatables.PetTypeTable;
import l1j.jrwz.server.model.L1World;
import l1j.jrwz.server.model.Instance.L1ItemInstance;
import l1j.jrwz.server.model.Instance.L1PcInstance;
import l1j.jrwz.server.model.Instance.L1PetInstance;
import l1j.jrwz.server.serverpackets.S_PetEquipment;
import l1j.jrwz.server.serverpackets.S_ServerMessage;
import l1j.jrwz.server.templates.L1PetItem;
import l1j.jrwz.server.templates.L1PetType;

// Referenced classes of package l1j.jrwz.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來使用寵物道具的封包
 */
public class C_UsePetItem extends ClientBasePacket {

    private static final String C_USE_PET_ITEM = "[C] C_UsePetItem";

    private static Logger _log = Logger.getLogger(C_UsePetItem.class.getName());

    @Override
    public void execute(byte[] decrypt, ClientThread client) {
        try {
            read(decrypt);
            L1PcInstance pc = client.getActiveChar();
            if (pc == null) {
                return;
            }
            if (pc.isDead()) {
                return;
            }
            if (pc.isGhost()) {
                return;
            }
            int data = readC();
            int petId = readD();
            int listNo = readC();

            L1PetInstance pet = (L1PetInstance) L1World.getInstance()
                    .findObject(petId);

            if (pet == null) {
                return;
            }
            L1ItemInstance item = pet.getInventory().getItems().get(listNo);
            if (item == null) {
                return;
            }

            if (item.getItem().getType2() == 0 // 类别：道具
                    && item.getItem().getType() == 11) { // petitem
                L1PetType petType = PetTypeTable.getInstance().get(
                        pet.getNpcTemplate().get_npcId());
                // 判断是否可用宠物装备
                if (!petType.canUseEquipment()) {
                    pc.sendPackets(new S_ServerMessage(74, item.getLogName())); // \f1%0%o无法使用。
                    return;
                }
                int itemId = item.getItem().getItemId();
                L1PetItem petItem = PetItemTable.getInstance().getTemplate(
                        itemId);
                if (petItem.getUseType() == 1) { // 牙齿
                    pet.usePetWeapon(pet, item);
                    pc.sendPackets(new S_PetEquipment(data, pet, listNo)); // 装备时更新宠物资讯
                } else if (petItem.getUseType() == 0) { // 盔甲
                    pet.usePetArmor(pet, item);
                    pc.sendPackets(new S_PetEquipment(data, pet, listNo)); // 装备时更新宠物资讯
                } else {
                    pc.sendPackets(new S_ServerMessage(74, item.getLogName())); // \f1%0%o无法使用。
                }
            } else {
                pc.sendPackets(new S_ServerMessage(74, item.getLogName())); // \f1%0%o无法使用。
            }
        } catch (final Exception e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            finish();
        }
    }

    @Override
    public String getType() {
        return C_USE_PET_ITEM;
    }

}
